<?php

namespace App\Filament\Resources\PostReviewResource\Pages;

use App\Filament\Resources\PostReviewResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Filament\Notifications\Notification;

class EditPostReview extends EditRecord
{
    protected static string $resource = PostReviewResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\Action::make('approve')
                ->label('批准发布')
                ->color('success')
                ->icon('heroicon-o-check-circle')
                ->requiresConfirmation()
                ->action(function () {
                    $this->record->update([
                        'is_approved' => true,
                        'approved_at' => now(),
                        'approved_by' => auth()->id(),
                    ]);

                    Notification::make()
                        ->title('文章已批准')
                        ->success()
                        ->send();

                    $this->redirect(PostReviewResource::getUrl('index'));
                }),

            Actions\Action::make('reject')
                ->label('拒绝发布')
                ->color('danger')
                ->icon('heroicon-o-x-circle')
                ->requiresConfirmation()
                ->form([
                    \Filament\Forms\Components\Textarea::make('rejected_reason')
                        ->label('拒绝原因')
                        ->required()
                        ->rows(3),
                ])
                ->action(function (array $data) {
                    $this->record->update([
                        'is_approved' => false,
                        'rejected_reason' => $data['rejected_reason'],
                        'status' => 'draft',
                    ]);

                    Notification::make()
                        ->title('文章已拒绝')
                        ->danger()
                        ->send();

                    $this->redirect(PostReviewResource::getUrl('index'));
                }),
        ];
    }

    public function getTitle(): string
    {
        return '审核文章: ' . $this->record->title;
    }

    protected function getSavedNotificationTitle(): ?string
    {
        return '审核信息已更新';
    }
}